﻿Public Class frmCarAED

    Private Sub btnDelete_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        Me.StartPosition = FormStartPosition.CenterScreen
        SA()
        Mechanic()
        LoadCarName()
        Status()
        LoadModelName()
        UI.FormatDataGrid(dgv)
    End Sub
    Public Sub LoadCarName()
        cboCarName.DisplayMember = "Briand_Name"
        cboCarName.ValueMember = "Briand_Id"
        cboCarName.DataSource = Cmd.ExecuteDataTable("select * from tblBriand where Active = 1")

    End Sub
    Public Sub LoadModelName()

        cboModelName.DisplayMember = "Model_Name"
        cboModelName.ValueMember = "Model_Id"
        cboModelName.DataSource = Cmd.ExecuteDataTable("select * from Model where  Active = 1")
    End Sub
    Public Sub SA()
        cboSA.DisplayMember = "SA_Name"
        cboSA.ValueMember = "SA_Id"
        cboSA.DataSource = Cmd.ExecuteDataTable("SELECT SA_name, SA_id FROM tblSA WHERE Active = 1")
    End Sub
    Public Sub Mechanic()
        cboMechanic.DisplayMember = "Mechanic_name"
        cboMechanic.ValueMember = "Mechanic_id"
        cboMechanic.DataSource = Cmd.ExecuteDataTable("SELECT Mechanic_id, Mechanic_name FROM tblMechanic WHERE Active = 1")
    End Sub
    Public Sub Status()
        cboStatus.DisplayMember = "Satus"
        cboStatus.ValueMember = "Satus_id"
        cboStatus.DataSource = Cmd.ExecuteDataTable("SELECT * FROM tblSatus WHERE Active = 1")
    End Sub
    Private Sub frmCarAED_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(dgv)
    End Sub

    Private Sub cboCarName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboCarName.SelectedIndexChanged
        'cboCarName.DataSource = Cmd.ExecuteDataTable("select B.Briand_id,Briand_Name ,Model_id,Model_Name from tblBriand B INNER JOIN Model M ON B.Briand_id = M.Briand_id where B.Active = 1")
        'cboModelName.DisplayMember = "Model_Name"
    End Sub
    Private Sub Parameters()
        Cmd.Parameters("@Briand_id") = Me.cboCarName.SelectedValue
        Cmd.Parameters("@Model_id") = Me.cboModelName.SelectedValue
        Cmd.Parameters("@Yearofcar") = Me.txtYear.Text
        Cmd.Parameters("@SA_id") = Me.cboSA.SelectedValue
        Cmd.Parameters("@Operation_No") = Me.txtOperation_No.Text
        Cmd.Parameters("@Mechanic_id") = Me.cboModelName.SelectedValue
        Cmd.Parameters("@Plate_Number") = Me.txtPlateNo.Text
        Cmd.Parameters("@Check_In") = Me.mtxtIn.Text
        Cmd.Parameters("@Check_out") = Me.mtxtOut.Text
        Cmd.Parameters("@Memo") = Me.txtMemo.Text
        Cmd.Parameters("@Satus_id") = Me.cboStatus.SelectedValue
    End Sub
    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Parameters()
        Cmd.ExecuteNonQuery("INSERT INTO tblCar VALUES (@Operation_No,@Briand_id,@Model_id,@Yearofcar,@Plate_Number,@SA_id,@Mechanic_id,@Check_In,@Check_out,@Memo,@Satus_id,1)")
        MsgBox("Data was save...!")
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub btnEdite_Click(sender As Object, e As EventArgs) Handles btnEdite.Click
        If txtOperation_No.Text = "" Then
            txtOperation_No.BackColor = Color.RosyBrown
            txtOperation_No.Focus()
            Exit Sub
        Else
            txtPlateNo.Text = ""
            txtPlateNo.BackColor = Color.RosyBrown
            txtPlateNo.Focus()
            Exit Sub
        End If


        If btnEdite.Text = "កែប្រែ" Then
            Parameters()
            Cmd.Parameters("@car_id") = Me.txtId.Text
            Cmd.ExecuteNonQuery("UPDATE tblCar SET Operation_No = @Operation_No,Briand_id = @Briand_id,Medel_Id = @Model_id, Yearofcar = @Yearofcar,Plate_Number = @Plate_Number,SA_id = @SA_id,Mechanic_id = @Mechanic_id,Check_In = @Check_In,Check_out = @Check_out,Memo = @Memo,Satus_id=@Satus_id where car_id = @car_id ")
            MsgBox("Data was updated...!")
        Else
            Dim i = Me.dgv.Rows.Add
            Dim row = Me.dgv.Rows(i)
            row.Cells("Operation_No").Value = Me.txtOperation_No.Text
            row.Cells("Briand_Name").Value = Me.cboCarName.SelectedValue
            row.Cells("Model_Name").Value = Me.cboModelName.SelectedValue
            row.Cells("Yearofcar").Value = Me.txtYear.Text
            row.Cells("SA_name").Value = Me.cboSA.SelectedValue
            row.Cells("Mechanic_Name").Value = Me.cboMechanic.SelectedValue
            row.Cells("Plate_Number").Value = Me.txtPlateNo.Text
            row.Cells("Check_In").Value = Me.mtxtIn.Text
            row.Cells("Check_Out").Value = Me.mtxtOut.Text
            row.Cells("Memo").Value = Me.txtMemo.Text
            row.Cells("Satus").Value = Me.cboStatus.SelectedValue
            clear()
        End If
    End Sub
    Public Sub clear()
        txtOperation_No.Text = ""
        'cboCarName.SelectedValue = ""
        'cboModelName.SelectedValue = ""
        txtYear.Text = ""
        'cboSA.SelectedValue = ""
        'cboMechanic.SelectedValue = ""
        txtPlateNo.Text = ""
        mtxtIn.Text = ""
        mtxtOut.Text = ""
        txtMemo.Text = ""
        'cboStatus.SelectedValue = ""
    End Sub

    Private Sub txtOperation_No_TextChanged(sender As Object, e As EventArgs) Handles txtOperation_No.TextChanged
        txtOperation_No.BackColor = Color.White
    End Sub

    Private Sub txtPlateNo_TextChanged(sender As Object, e As EventArgs) Handles txtPlateNo.TextChanged
        txtPlateNo.BackColor = Color.White
    End Sub
End Class